package controlller;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

import comm.User;
import javafx.application.Platform;
import javafx.fxml.Initializable;

public class ControllerManager {
	private static HashMap<String,Initializable> controllerMap=new HashMap<>();
	private static TopControlPaneController right=null;
	private static DateTimeFormatter formatter=DateTimeFormatter.ofPattern("kk:mm:ss");
	
	public static void add(String key,Initializable controller) {
		controllerMap.put(key, controller);
	}
	
	public static RightControlPaneController getRight() {
		return (RightControlPaneController)get("rightPane");
	}
	
	public static TableViewController getTable() {
		return (TableViewController)get("qqInfoTable");
	}
	
	public static TopControlPaneController getTop() {
		return (TopControlPaneController)get("top");
	}
	private static Initializable get(String key) {
		return controllerMap.get(key);
	}

	public static void appendCmd(User user,String info) {
		RightControlPaneController right = getRight();
		Platform.runLater(() -> {
			outTime();
			right.appendCmd((user == null ? "" : user.getQq()) + info);
		});
	}
	

	/**
	 * @return the formatter
	 */
	private static DateTimeFormatter getFormatter() {
		return formatter;
	}
	
	private static void outTime() {
		getRight().appendCmd(LocalTime.now().format(getFormatter())+":");
	}
	
	public static void updateProgress() {
		Platform.runLater(() -> {
			getTop().updateProgress();
		});
	}
	
	public static void updateLabel() {
		Platform.runLater(() -> {
			getTop().updateLabel();
		});
	}
}
